package cn.hwadee.muying.controller;


import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.enums.Status;
import cn.hwadee.muying.service.WorkerService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.ui.Model;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.service.ReceptionService;
import cn.hwadee.muying.entity.Reception;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.authz.annotation.Logical;

/**
* <p>
* 接待情况 前端控制器
*
* </p>
*
* @author Mankind
* @since 2018-08-06
*/
@Controller
@RequestMapping("/sys/reception")
public class ReceptionController extends BaseController {
    @Autowired
    private ReceptionService receptionService;
    @Autowired
    private WorkerService workerService;
    @ModelAttribute("reception")
    public Reception get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return receptionService.selectById(id);
        }else{
            return new Reception();
        }
    }

    @RequiresPermissions("sys:reception:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        model.addAttribute("statuses",Status.values());
        return "/biz/reception/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:reception:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list(Integer status) throws Exception{
        MyPage<Reception> page = getPage();
        page = receptionService.list(page,getCurrentUser(),status);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑接待情况界面
     * @param reception
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:reception:edit","sys:reception:add"},logical=Logical.OR)
    @RequestMapping(value = {"/edit","/add"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Reception reception,Model model) throws Exception{
        model.addAttribute("reception",reception);
        return "/biz/reception/edit";
}

    /**
     * 处理更新接待情况的请求
     * @param reception
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:reception:edit","sys:reception:add"},logical=Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Reception reception){
        Map<String,Object> result = new HashMap();
        try {
        if(reception.getId() != null && reception.getId() > 0){
            receptionService.updateById(reception);
        }else {
            reception.setCustomId(getCurrentUser().getId());
            reception.setOrgId(getCurrentUser().getOrgId());
            receptionService.insert(reception);
        }
        result.put("status",200);
        result.put("msg","接待情况信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","接待情况信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除接待情况的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:reception:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap();
        try {
        if(id != null && id > 0){
            receptionService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除接待情况成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的接待情况");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除接待情况失败");
        }
        return result;
    }


    /**
     * 跳转分配跟进职员界面
     * @param reception
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:reception:allot"},logical=Logical.OR)
    @RequestMapping(value = {"/allot"}, produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String allot(@ModelAttribute Reception reception,Model model) throws Exception{
        model.addAttribute("reception",reception);
        model.addAttribute("followers",workerService.selectFollowerList(getCurrentUser().getOrgId()));
        return "/biz/reception/allot";
    }

    /**
     * 处理分配跟进职员的请求
     * @param reception
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value={"sys:reception:allot"},logical=Logical.OR)
    @RequestMapping(value = "/allot", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> allot(@ModelAttribute Reception reception){
        Map<String,Object> result = new HashMap();
        try{
            reception.setStatus(Status.FIRST_FOLLOW);
            receptionService.updateById(reception);
            result.put("status",200);
            result.put("msg","分配跟进职员成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","分配跟进职员失败");
        }
        return result;
    }
}
